package com.sky.service.admin.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.RedisConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.mapper.admin.CategoryMapper;
import com.sky.pojo.Category;
import com.sky.result.PageResult;
import com.sky.service.admin.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private RedisTemplate redisTemplate;


    /**添加新增菜品分来和新增套餐分类*/
    @Override
    public void insertCategoryService(Category category) {


        categoryMapper.insertCategory(category);


        redisTemplate.delete(RedisConstant.CATEGORY_LIST+category.getType());

    }


   /** 进行菜品套餐分类页面分页查询-name模糊查询*/
    @Override
    public PageResult selectCategoryService(CategoryPageQueryDTO categoryPageQueryDTO) {

        PageHelper.startPage(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize());

        Integer type = categoryPageQueryDTO.getType();
        String name = categoryPageQueryDTO.getName();


        List<Category> categories = categoryMapper.selectCategory(name, type);
        Page<Category> page = (Page<Category>) categories;


        return new PageResult(page.getTotal(), page.getResult());
    }

    /**根据id删除行数据*/
    @Transactional
    @Override
    public void deleteCategoryService(Long id) {

        Integer i = categoryMapper.selectType(id);


        categoryMapper.deleteCategory(id);

        
        redisTemplate.delete(RedisConstant.CATEGORY_LIST+i);




    }


   /**修改数据*/

    @Override
    public void updateCategoryServise(Category category) {

        categoryMapper.updateCategory(category);

        Integer i = categoryMapper.selectType(category.getId());
        redisTemplate.delete(RedisConstant.CATEGORY_LIST+i);
    }


    /**启用或禁用分类行数据status*/
    @Override
    public void updateCategoryStatus(Integer status, Integer id) {
        categoryMapper.updateCategoryStatus(id, status);


        Integer i = categoryMapper.selectType(id);
        redisTemplate.delete(RedisConstant.CATEGORY_LIST+i);


    }




   /**查询菜品分类名称给进行菜品管理添加*/
    @Override
    public List<Category> selectNameService(Integer type) {

        List<Category> list= (List<Category>) redisTemplate.opsForValue().get(RedisConstant.CATEGORY_LIST +type);
        if (list==null || list.size()==0){
            list=categoryMapper.selectName(type);

          redisTemplate.opsForValue().set(RedisConstant.CATEGORY_LIST+type,list);


        }
        return list;
    }
}
